﻿
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei
{
    public partial class AddIPQCData_ : UIForm
    {
        public AddIPQCData_()
        {
            InitializeComponent();
            GetPRQCData();
            ShowIPQCTypeData();
        }

        #region 定义检测类型
        /// <summary>
        /// 定义检测类型数据
        /// </summary>sh
        public async void ShowIPQCTypeData()
        {
            List<IPQCTypeData> iPQCTypeData = new List<IPQCTypeData>
            {
                new IPQCTypeData { IPQCTypeId=0,IPQCTypeName="请选择检测类型"},
                new IPQCTypeData { IPQCTypeId=1,IPQCTypeName="进场检测"},
                new IPQCTypeData { IPQCTypeId=2,IPQCTypeName="宰前检测"},
                new IPQCTypeData { IPQCTypeId=3,IPQCTypeName="宰后检测"},
                new IPQCTypeData { IPQCTypeId=4,IPQCTypeName="肉质品质检测"},
                new IPQCTypeData { IPQCTypeId=5,IPQCTypeName="其他检测"},
            };
            IPQCType.DataSource = iPQCTypeData;
            IPQCType.DisplayMember = "IPQCTypeName";
            IPQCType.ValueMember = "IPQCTypeName";
        }

        /// <summary>
        /// 定义检测类型类
        /// </summary>
        public class IPQCTypeData
        {
            public int IPQCTypeId { get; set; }
            public string IPQCTypeName { get; set; }
        }
        #endregion

        #region 获取检测项目下拉框数据
        List<SHowPRQCData> sHowPRQCDatas = new List<SHowPRQCData>();
        public async void GetPRQCData()
        {
            //http://localhost:5106/api/Tsw/GetIPQCData
            var res = await HttpClientHelper.RequestUrl<SHowPRQCData, ApiResult<List<SHowPRQCData>>>(URLCommon.Url2, "/Tsw/GetIPQCData", HttpClientHelper.MethodType.GET);
            if (res != null)
            {
                if (res.Code > 0)
                {
                    sHowPRQCDatas = res.Data;
                    PRQSName.DataSource = res.Data;
                    //PRQSName.ValueMember = "PRQSName";
                    PRQSName.DisplayMember = "PRQSName";
                }
            }
        }
        /// <summary>
        /// 获取检测项目下拉框数据
        /// </summary>
        public class SHowPRQCData
        {
            /// <summary>
            /// 检疫项目Id
            /// </summary>
            public int PRQSId { get; set; }
            /// <summary>
            /// 检疫项目名称
            /// </summary>
            public string PRQSName { get; set; } = string.Empty;
            /// <summary>
            /// 检疫指标
            /// </summary>
            public string PRQSindex { get; set; } = string.Empty;
            /// <summary>
            /// 标准要求
            /// </summary>
            public string Require { get; set; } = string.Empty;
            /// <summary>
            /// 启用状态
            /// </summary>
            public bool PRQSstate { get; set; } = false;
            /// <summary>
            /// 创建人
            /// </summary>
            public string CreateName { get; set; } = string.Empty;
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime CreateTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 最后修改人
            /// </summary>
            public string FinallyName { get; set; } = string.Empty;
            /// <summary>
            /// 最后修改时间
            /// </summary>
            public DateTime FinallyTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 是否禁用
            /// </summary>
            public bool IsPRQS { get; set; } = false;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool ISDeletePRQS { get; set; } = false;
        }
        #endregion

        /// <summary>
        /// 反填检测项目数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PRQSlistData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!(e.RowIndex >= 0))
            {
                return;
            }
            var PRQSDataName = PRQSlistData.Rows[e.RowIndex].Cells[1].Value.ToString();
            var PRQSContext = sHowPRQCDatas.FirstOrDefault(x => x.PRQSName == PRQSDataName);
            if (PRQSContext != null)
            {
                PRQSlistData.Rows[e.RowIndex].Cells[0].Value = PRQSContext.PRQSId;
                PRQSlistData.Rows[e.RowIndex].Cells[2].Value = PRQSContext.PRQSindex;
                PRQSlistData.Rows[e.RowIndex].Cells[3].Value = PRQSContext.Require;
            }
        }

        #region 添加检测标准
        private async void uiButton2_Click(object sender, EventArgs e)
        {
            StringBuilder PRQSData = new StringBuilder();
            int PRQSNum = PRQSlistData.Rows.Count - 2;
            for (int i = PRQSNum; i >= 0; i--)
            {
                PRQSData.Append(PRQSlistData.Rows[i].Cells[1].Value.ToString() + ",");
            }
            //for(int i=)
            //定义检测标准备注表
            List<IPQCRemarkData> iPQCRemarkDatas = new List<IPQCRemarkData>();
            //定义检测标准表
            AddIPQCData addIPQCData = new AddIPQCData()
            {
                IPQCId = 0,
                LiveAnimalsDocument = LiveAnimelsDocument.Text,
                LiveAnimalsDocumentTIme = DateTime.Parse(LiveAnimelsDocumentTime.Text),
                IPQCName = IPQCName.Text,
                IPQCType = IPQCType.Text,
                ExecuteIPQC = ExecuteIPQS.Text,
                UseGoodsName = "",
                IsIPQC = IsIPQC.Active,
                IPQCAuditState = IsIPQC.Active,
                IPQCCreateName = "",
                AuditStateTIme = DateTime.Now,
                PRQSName = PRQSData.ToString(),
                IsDeleteIPQC = false
            };
            //添加检测标准表端口
            //http://localhost:5159/api/TSW/AddIPQCData
            var res = await HttpClientHelper.RequestUrl<AddIPQCData, ApiResult<AddIPQCData>>(URLCommon.Url1, "/TSW/AddIPQCData", HttpClientHelper.MethodType.POST, addIPQCData);
            if (res.Code > 0)
            {
                IPQCRemarkData addIPQCRemarkData = new IPQCRemarkData();
                for (int i = PRQSNum; i >= 0; i--)
                {
                    addIPQCRemarkData.PRQSId.Add(Convert.ToInt32(PRQSlistData.Rows[i].Cells[0].Value.ToString()));
                    addIPQCRemarkData.IPQCRemarkData_.Add(PRQSlistData.Rows[i].Cells[4].Value.ToString());
                }
                addIPQCRemarkData.IPQCId = res.Code;
                addIPQCRemarkData.IsIPQCRemarkData = false;
                addIPQCRemarkData.IPQCRemarkDataId = 0;
                //添加检测标准备注表端口
                //http://localhost:5159/api/TSW/AddIPQCDataRemarkData
                var Result = await HttpClientHelper.RequestUrl<IPQCRemarkData, ApiResult<IPQCRemarkData>>(URLCommon.Url1, "/TSW/AddIPQCDataRemarkData", HttpClientHelper.MethodType.POST, addIPQCRemarkData);
                if (Result != null)
                {
                    if (Result.Code > 0)
                    {
                        MessageBox.Show("添加成功");
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        private void uiPanel4_Click(object sender, EventArgs e)
        {
            uiButton2_Click(sender, e);
        }
        /// <summary>
        /// 定义检测标准备注
        /// </summary>
        public class IPQCRemarkData
        {
            /// <summary>
            /// 检测标准备注ID
            /// </summary>
            public int IPQCRemarkDataId { get; set; }
            /// <summary>
            /// 检测标注ID
            /// </summary>
            public int IPQCId { get; set; }
            /// <summary>
            /// 备注信息
            /// </summary>
            public List<string> IPQCRemarkData_ { get; set; }= new List<string>();
            /// <summary>
            /// 检疫项目ID
            /// </summary>
            public List<int> PRQSId { get; set; } =new List<int>();
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsIPQCRemarkData { get; set; }
        }
        public class APIReusltDta
        {
            public int Data { get; set; }
        }
        /// <summary>
        /// 定义添加检测标准类
        /// </summary>
        public class AddIPQCData
        {
            /// <summary>
            /// 检疫标准Id
            /// </summary>
            public int IPQCId { get; set; }
            /// <summary>
            /// 单据Id
            /// </summary>
            public string LiveAnimalsDocument { get; set; } = string.Empty;
            /// <summary>
            /// 单据日期
            /// </summary>
            public DateTime LiveAnimalsDocumentTIme { get; set; } = DateTime.Now;
            /// <summary>
            /// 标准名称
            /// </summary>
            public string IPQCName { get; set; } = string.Empty;
            /// <summary>
            /// 检疫类型
            /// </summary>
            public string IPQCType { get; set; } = string.Empty;
            /// <summary>
            /// 执行标准
            /// </summary>
            public string ExecuteIPQC { get; set; } = string.Empty;
            /// <summary>
            /// 应用商品
            /// </summary>
            public string UseGoodsName { get; set; } = string.Empty;
            /// <summary>
            /// 开启状态
            /// </summary>
            public bool IsIPQC { get; set; } = false;
            /// <summary>
            /// 审核状态
            /// </summary>
            public bool IPQCAuditState { get; set; } = false;
            /// <summary>
            /// 制单人
            /// </summary>
            public string IPQCCreateName { get; set; } = string.Empty;
            /// <summary>
            /// 制单时间
            /// </summary>
            public DateTime AuditStateTIme { get; set; } = DateTime.Now;
            /// <summary>
            /// 检疫项目
            /// </summary>
            public string PRQSName { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDeleteIPQC { get; set; } = false;
        }
        #endregion

        /// <summary>
        /// 关闭这个页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton5_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton3_Click(object sender, EventArgs e)
        {
            LiveAnimelsDocumentTime.Text = "";
            IPQCName.Text = "";
            ExecuteIPQS.Text = "";
        }
    }
}
